/*------------------------------

	Space Is The Place v 0.1
		by Sestze

------------------------------*/

-- reflected-sounds-of-underground-spirits

SITP.EconEnts = { 	"acf_gun",
					"acf_ammo", 
					"sitp_miningtool",
					"sitp_core"			}

SITP.EconMult = {}

SITP.EconMult["MG"] = 1
SITP.EconMult["AC"] = 2
SITP.EconMult["HMG"] = 2
SITP.EconMult["RAC"] = 8
SITP.EconMult["C"] = 0.35
SITP.EconMult["AL"] = 0.425
SITP.EconMult["HW"] = 0.35
SITP.EconMult["MO"] = 0.35
SITP.EconMult["GL"] = 0.5

SITP.EconMult["Calibre"] = 500

function SITP_FormalName ( str )
	if(str == "sitp_core") then return "core" end
	if(str == "acf_gun") then return "gun" end
	if(str == "acf_ammo") then return "ammo" end
	if(str == "sitp_miningtool") then return "mining laser" end
end

function SITP_ApplyCost ( ent )
	if ( SinglePlayer() == true ) then return end
	if (ent.sitp_ownid == nil) then return end
	if (ent.sitp_cost == 0 or ent.sitp_cost == nil) then return end
	local ply = GetPlayerBySteamID( ent.sitp_ownid )
	
	if( ply and ply:IsValid() ) then
		local creds = ply:GetNetworkedInt( "sitp_creds" )
		if(SITP_DeductCreds(ply, ent.sitp_cost, ent:GetClass()) == false) then
			ply:ChatPrint( "You don't have " .. tostring(ent.sitp_cost) .. " credits for this " .. SITP_FormalName(ent:GetClass()) .. "!" )
			ent:Remove()
		else
			creds = ply:GetNetworkedInt( "sitp_creds" )
			ply:ChatPrint( tostring(ent.sitp_cost) .. " credits deducted for a " .. SITP_FormalName(ent:GetClass()) .. ". You now have " .. tostring(creds) .. " in your balance." )
		end
	else
		ent:Remove()
	end
end

function SITP_AffixCost ( ent )
	if ( SinglePlayer() == true ) then return end
	if ( !ent or !ent:IsValid() ) then return end
	
	local ply = ent.Owner
	if( !ply or !ply:IsValid() ) then
		return
	end
	local team = ply.sitp_team
	ent.sitp_ownid = ply:SteamID()
	ent.sitp_cost = 0
	
	local corpmul = 1
	
	if( table.HasValue( SITP.EconEnts, ent:GetClass() ) ) then
		if (ent:GetClass() == "acf_gun") then
			if(team == "fsa") then corpmul = 0.8 end
			ent.sitp_cost = SITP.EconMult[ent.Class] * SITP.EconMult["Calibre"] * (ent.Caliber / 2)^2 * corpmul
		elseif (ent:GetClass() == "acf_ammo") then
			if(team == "fsa") then corpmul = 0.8 end
			local phys = ent:GetPhysicsObject()
			if(phys and phys:IsValid()) then
				ent.sitp_cost = phys:GetMass() * corpmul / 2
			end
		elseif (ent:GetClass() == "sitp_miningtool") then
			if(team == "gmc") then corpmul = 0.8 end
			ent.sitp_cost = 5 * (math.sqrt(9.5) * math.sqrt(math.max(ent.DPS, 0))) * 100 * corpmul
		elseif (ent:GetClass() == "sitp_core") then
			if(team == "gmc") then corpmul = 0.8 end
			ent.sitp_cost = 5 * (math.sqrt(4.5) * math.sqrt(math.max(ent.TonGrade, 0))) * 100 * corpmul
		end
		ent.sitp_cost = math.floor(math.max(ent.sitp_cost, 0))
		local creds = ply:GetNetworkedInt( "sitp_creds" )
		ply:ChatPrint( "This entity has a cost of " .. tostring(ent.sitp_cost) .. " credits. You currently have " .. tostring(creds) .. " credits.")
	end
end

function SITP_InitAffix( ent )
	if !ent or !ent:IsValid() then return end
	timer.Create("affixto" .. tostring(ent:EntIndex()), 0.1, 1, SITP_AffixCost, ent)
end

hook.Add( "OnEntityCreated", "Affix Cost for SITP", SITP_InitAffix )

function SITP_FindCost ( ent )
	local allents = constraint.GetAllConstrainedEntities( self.Entity )
	local totalcost = ent.sitp_cost
	for k, v in pairs (allents) do
		if(v:IsValid() and v) then
			totalcost = totalcost + v.sitp_cost
		end
	end
	return totalcost
end